package star.oath.project.battle.util;

import star.oath.project.battle.bean.BattleRoundData;
import star.oath.project.battle.bean.BattleRoundResult;
import star.oath.project.battle.bean.Dice;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class BattleUtils {

    /**
     * 获取本轮攻击结果
     * @param attacker
     * @param defender
     * @return
     */
    public static BattleRoundResult getAttackResult(BattleRoundData attacker, BattleRoundData defender) {
        //1.计算伤害
        int totalDamage = 0;
        int effectiveDamage = 0;
        Map<String, Integer> damages = new HashMap<>();
        Map<String, Dice> attacks = attacker.getAttacks();
        for (Map.Entry<String, Dice> entry : attacks.entrySet()) {
            String elementType = entry.getKey();
            int damage = getSingleDamage(attacker, defender, elementType);
            damages.put(elementType, damage);
            totalDamage = totalDamage + damage;
        }
        //2.计算格挡
        boolean isBlk = (new Random().nextInt(100) + 1) < defender.getBlk();
        if (isBlk) {
            int blkDmg = defender.getBlkDmg();
            effectiveDamage = totalDamage - blkDmg;
        }
        //3.计算闪避
        boolean isAvd = (new Random().nextInt(100) + 1) < defender.getAvd();
        if (isAvd) {
            effectiveDamage = 0;
        }
        //4.返回结果
        BattleRoundResult result = new BattleRoundResult();
        result.setAttackerName(attacker.getName());
        result.setDefenderName(defender.getName());
        result.setTotalDamage(totalDamage);
        result.setDamages(damages);
        result.setIsBlk(isBlk);
        result.setIsAvd(isAvd);
        result.setEffectiveDamage(effectiveDamage);
        return result;
    }

    /**
     * 获取单次伤害
     * @param attacker
     * @param defender
     * @param elementType
     * @return
     */
    private static int getSingleDamage(BattleRoundData attacker, BattleRoundData defender, String elementType) {
        double damage = 0;
        //1.计算骰子点数
        Dice dice = attacker.getAttacks().getOrDefault(elementType,new Dice());
        int diceNumber = dice.getDiceNumberOrDefault();
        int diceSides = dice.getDiceSidesOrDefault();
        int diceBonus = dice.getDiceBonusOrDefault();
        for (int i = 1; i <= diceNumber; i++) {
            int rollNumber = new Random().nextInt(diceSides) + 1;
            damage = damage + rollNumber;
        }
        damage = damage + diceBonus;
        //2.计算暴击
        boolean isCrt = (new Random().nextInt(100) + 1) < attacker.getCrt();
        if (isCrt) {
            int extra = attacker.getExtras().getOrDefault(elementType, 0);
            damage = damage * (130+extra)/100;
        }
        //3.计算减免
        int defense = defender.getDefences().getOrDefault(elementType, 0);
        damage = damage - defense;
        //4.计算穿透
        int pierce = attacker.getPierces().getOrDefault(elementType, 0);
        int resistance = defender.getResistances().getOrDefault(elementType, 0);
        damage = damage * (100-resistance+pierce)/100;
        //5.计算吸收
        int absorb = defender.getAbsorbs().getOrDefault(elementType, 0);
        damage = damage * (100-absorb)/100;
        //6.返回伤害
        damage = damage < 0 ? 0 : damage;
        return (int) damage;
    }


}
